package com.dfrz.problem.service.impl;

import java.util.Iterator;
import java.util.List;

import com.dfrz.problem.dao.EmployeeDao;
import com.dfrz.problem.dao.ProjectDao;
import com.dfrz.problem.model.Employee;
import com.dfrz.problem.model.ProjectInfo;
import com.dfrz.problem.service.ProjectService;
import com.dfrz.problem.utils.IConstant;
import com.dfrz.problem.utils.Result;

public class ProjectServiceImpl implements ProjectService{
	private ProjectDao projectDao;
	private EmployeeDao employeeDao;

	public ProjectDao getProjectDao() {
		return projectDao;
	}

	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}

	@Override
	public void addProject(ProjectInfo project) {
		projectDao.addProjectInfo(project);
	}

	@Override
	public Result<List<ProjectInfo>> getListProject(Integer pmId) {
		return null;
	}

	@Override
	public Result<ProjectInfo> getProjectOnId(int projectId) {
		Result<ProjectInfo> result = new Result<ProjectInfo>();
		result.setResult(projectDao.getProjectInfo(projectId));
		return result;
	}
	
	@Override
	public Result<List<ProjectInfo>> getAllProjects() {
		Result<List<ProjectInfo>> result = new Result<List<ProjectInfo>>();
		List<ProjectInfo> list = projectDao.getAllProjectInfos();
		if(list == null || list.size() == 0){
			result.setCode(IConstant.PROJECT_NOT_EXIST);
			return result;
		}
		result.setResult(list);
		return result;
	}

	@Override
	public void closeOrOpenProject(ProjectInfo projectInfo) {
		if (projectInfo.getProjectStatus() == 1) {
			projectInfo.setProjectStatus(2);
			projectDao.updateProject(projectInfo);
		}else if (projectInfo.getProjectStatus() == 2) {
			projectInfo.setProjectStatus(1);
			projectDao.updateProject(projectInfo);
		}
	}
	
	@Override
	public void removePlayers(List<Employee> list,Integer projectId){
		
		Iterator<Employee> it = list.iterator();
		// 获取默认项目信息
		ProjectInfo project = getProjectOnId(projectId).getResult(); 
		while(it.hasNext()){
			Employee employee = it.next();
			employee.setProject(project);
			employeeDao.updateEmployeeProject(employee);
		}
	}
	@Override
	public void removePlayer(Integer employeeId){
		Employee employee = employeeDao.getEmployee(employeeId);
		employee.setProject(projectDao.getProjectInfo(1));
		employeeDao.updateEmployee(employee);
	}
	@Override
	public void deleteProject(ProjectInfo projectInfo) {
		projectDao.deleteProject(projectInfo);
	}
	
	@Override
	public void updateProject(ProjectInfo projectInfo) {
		projectDao.updateProject(projectInfo);
	}
	
	public void setEmployeeDao(EmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}
}
